﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./Huyen.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 1000;
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public string type = "";
    public string strProcessName = "";
    public int nPermissions = 0;
    public int count = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Province"] != null && int.Parse(Session["Province"].ToString()) > 0)
        {
            if (Session["Province"] != "") nPermissions = int.Parse(Session["Province"].ToString());
        }
        else
            Response.Redirect("Login.aspx");
        int nPage = 1;
        string strPageID = "page_number";


        strProcessName = "CUS";

        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;
        if (Session["changLoadingProvince"] == null) Session["changLoadingProvince"] = "";
        //if (Session["logined_id"] != null)
        //{
        //}
        //else
        //    Response.Redirect("Login.aspx");

        if (!Page.IsPostBack)
        {
            if (Request.QueryString["TinhID"] != null)
            {
                Session["changLoadingProvince"] = Request.QueryString["TinhID"].ToString();
            }
            loadDropdownTinh();
            loadDropdownLoadingPort();
            if (Request.QueryString["action"] == "delete")
            {
                Huyen obj = new Huyen();
                obj.ID = long.Parse(Request.QueryString["ID"]);
                obj.Load(GV.cn, GV.trans);
                obj.Delete(GV.cn, GV.trans);
                UserObjectsLog.SaveObjectActionLog(long.Parse(Session["logined_id"].ToString()), "huyen", "delete", GV.cn, GV.trans);
                Response.Redirect("Huyen.aspx?" + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "page_number=" + Request.QueryString["page_number"].ToString() : ""));
            }
            else
            {
                if (Request.QueryString["ID"] != null)
                {
                    loadHuyen();
                }
            }

        }

    }
    protected void loadHuyen()
    {
        Huyen obj = new Huyen();
        obj.ID = long.Parse(Request.QueryString["ID"]);
        obj.Load(GV.cn, GV.trans);
        txtTen.Text = HttpUtility.HtmlDecode(obj.Ten);
        txtKms.Text = obj.Kms.ToString();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        if (txtTen.Text == "")
        {
            strError = "NotBlank";
            strMess = "Tên huyện không được để trống!";
            lblerror.Text = strMess;
            return;
        }
        if (drTinh.SelectedItem.Value == "")
        {
            strError = "NotBlank";
            strMess = "Bạn phải chọn tỉnh!";
            lblerror.Text = strMess;
            return;
        }
        Huyen obj = new Huyen();
        obj.ID = -1;
        if (Request.QueryString["ID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["ID"]);
            obj.Load(GV.cn, GV.trans);
        }

        obj.Ma = DBModule.UnicodeToPlain(txtTen.Text.Trim());
        obj.Ten = txtTen.Text.Trim();
        try
        {
            obj.Kms = float.Parse(txtKms.Text.Trim());
        }
        catch { }
        try
        {
            obj.TinhID = long.Parse(drTinh.SelectedItem.Value);
        }
        catch { }
        if (strError == "")
        {
            obj.Save(GV.cn, GV.trans);

            DataSet ds = null;
            string sqlCommand = "SELECT tbl_tinh.Ten AS TenTinh, tbl_huyen.Ten AS TenHuyen FROM tbl_tinh,tbl_huyen WHERE tbl_huyen.TinhID =tbl_tinh.ID;";
            ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
            string strName = "";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string strHuyen = DBModule.ucFirstAllWords(dr["TenHuyen"].ToString().Trim());
                string strTinh = DBModule.ucFirstAllWords(dr["TenTinh"].ToString().Trim());
                if (strName.Length > 0) strName += ",";
                strName += "\"" + DBModule.UnicodeToPlain(strHuyen.Trim()) + ", " + DBModule.UnicodeToPlain(strTinh.Trim()) + "\"";
            }
            string strCountFile = Server.MapPath("./") + "\\Assets\\Js\\DomesticPOR.js";
            StreamWriter sw;

            strName = "var por = [" + strName + "];";
            sw = new StreamWriter(strCountFile, false);
            sw.Write(strName);
            sw.Close();
        }
    }
    protected void LoadTinh()
    {
        DataSet ds = null;
        int nNumOfPages = 0;
        int NumOfItemsPerPage = 100;
        int PageNo = 1;

        if (Session["changLoadingProvince"].ToString().Length > 0)
        {
            strCondition += " AND Tbl_Tinh.ID='" + Session["changLoadingProvince"].ToString() + "'";
        }
        Tinh obj = new Tinh();
        obj.GetList(strCondition, "Ten ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

        Response.Write("<tbody>");
        if (ds != null)
        {
            count = 0;

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                //count++;
                long strID = int.Parse(dr["ID"].ToString());
                string strTenTinh = dr["Ten"].ToString();
                string strLocationName = dr["LocationName"].ToString();
                //Response.Write("<tr style=\"background-color:#ececec;\">");
                //Response.Write("<td><p class='wrapword' style=\"font-weight:bold;\" >" + index + strTenTinh + "</td>");
                //Response.Write("<td style=\"text-align:center;width:120px;\"></td>");
                //Response.Write("</tr>");
                LoadHuyen(strID.ToString(), strTenTinh, strLocationName);
            }

        }
        Response.Write("</tbody>");

    }
    protected void LoadHuyen(string TinhID, string strTenTinh, string LocationName)
    {
        DataSet ds = null;
        int nNumOfPages = 0;
        int NumOfItemsPerPage = 100;
        int PageNo = 1;

        //if (Session["changLoadingPort"].ToString().Length > 0)
        {

            Huyen obj = new Huyen();
            obj.GetList("", TinhID, "Ten ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

            if (ds != null)
            {
                //int count = 0;

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    count++;
                    long strID = int.Parse(dr["ID"].ToString());
                    string strTen = dr["Ten"].ToString();
                    string strKms = dr["Kms"].ToString();
                    string strAction = "";
                    if (nPermissions >= 3)
                        strAction = "<a href=\"Huyen.aspx?ID=" + strID + "&TinhID=" + TinhID + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : "") + "\">Edit</a>";
                    if (nPermissions >= 9)
                        strAction += " - <a href=\"javascript:doDelete('" + System.Web.HttpUtility.HtmlEncode(strID.ToString()) + "')\">" + "Delete</a>";
                    Response.Write("<tr onmouseover=\"this.style.background='#dbf0c9'\" onmouseout=\"this.style.background='#ffffff'\">");
                    Response.Write("<td>" + count + "</td>");
                    Response.Write("<td>" + strTen + "</td>");
                    Response.Write("<td >" + strTenTinh + "</td>");
                    Response.Write("<td >" + LocationName + "</td>");
                    Response.Write("<td >" + strKms + "</td>");
                    Response.Write("<td style=\"text-align:center;width:120px;\">" + strAction + "</td>");
                    Response.Write("</tr>");
                }
            }
        }
    }
    protected void loadDropdownTinh()
    {
        string sql = "";
        DataSet ds = null;

        sql = "SELECT ID,Ten FROM tbl_tinh order by Ten";
        ds = DBModule.ExecuteQuery(sql, GV.cn, GV.trans);
        DataTable dtb = ds.Tables[0];
        drTinh.DataSource = dtb;
        drTinh.DataValueField = dtb.Columns[0].ToString();
        drTinh.DataTextField = dtb.Columns[1].ToString();
        drTinh.DataBind();
        drTinh.Items.Insert(0, new ListItem("chọn tỉnh", ""));
        if (Session["changLoadingProvince"] != null && ds.Tables[0].Rows.Count > 0)
        {
            try
            {
                drTinh.SelectedValue = Session["changLoadingProvince"].ToString();
            }
            catch { }
        }
    }
    protected void loadDropdownLoadingPort()
    {
        string sql = "";
        DataSet ds = null;

        sql = "SELECT ID,Ten FROM tbl_tinh";
        ds = DBModule.ExecuteQuery(sql, GV.cn, GV.trans);
        DataTable dtb = ds.Tables[0];
        drLoadingProvince.DataSource = dtb;
        drLoadingProvince.DataValueField = dtb.Columns[0].ToString();
        drLoadingProvince.DataTextField = dtb.Columns[1].ToString();
        drLoadingProvince.DataBind();
        drLoadingProvince.Items.Insert(0, new ListItem("Loading Province", ""));
        if (Session["changLoadingProvince"] != null && ds.Tables[0].Rows.Count > 0)
        {
            try
            {
                drLoadingProvince.SelectedValue = Session["changLoadingProvince"].ToString();
            }
            catch { }
        }
    }
    protected void drLoadingProvince_Selected(object sender, EventArgs e)
    {
        Session["changLoadingProvince"] = drLoadingProvince.SelectedValue;
        Response.Redirect("Huyen.aspx");
    }
}
